package com.wkck.controller.admin;

import com.wkck.annotaion.ClearAllCache;
import com.wkck.result.CommonResult;
import com.wkck.result.Result;
import com.wkck.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @Author: CK
 * @Date: 2024/02/10/17:46
 * @Description: 通用接口
 */
@RestController
@RequestMapping("/common")
@Slf4j
@Api(value = "通用接口", tags = "通用接口")
@CrossOrigin
public class CommonBgController {

    @Autowired
    private CommonService commonService;

    /**
    * @Author: CK
    * @Date: 2024/2/10
    * @Param: []
    * @Description: 提交图片
    */
    @ApiOperation(value = "提交图片")
    @PostMapping("/commitPhoto/{uuid}")
    public CommonResult<Boolean> commitPhoto(@RequestPart MultipartFile file, @PathVariable("uuid") String uuid) throws IOException {
        log.info("file: {}, uuid: {}", file, uuid);
        //创建一个临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        file.transferTo(tempFile);
        // 文件路径
        String localFilePath = tempFile.getAbsolutePath();
        // 文件大小
        long size = file.getSize();
        // 原始文件名称
        String filename = file.getOriginalFilename();
        log.info("{}, {}, {}", localFilePath, size, filename);
        return CommonResult.success(commonService.commitPhoto(localFilePath, filename, size, uuid));
    }

    /***
    * @Author: CK
    * @Date: 2024/2/12
    * @Param: [url]
    * @Description: 需要删除的图片
    */
    @ApiOperation(value = "需要删除的图片")
    @PostMapping("/deletePhoto")
    @ClearAllCache
    public Result<Boolean> deletePhoto(@RequestBody Map<String, String> url) {
        log.info("需要删除的图片: {}",  url);
        return Result.success(commonService.deletePhotoByUrl(url.get("url")));
    }


}
